<?php


namespace XinYaTable\Forms\Models;


class EloquentFormFilterGroup extends BaseModel {
    /**
     * @var array
     */
    protected $defaultListFields = [
        'id',
        'form_id',
        'can_edit',
        'is_system',
        'title',
        'uid',
        'order',
    ];
    
    /**
     * @var string
     */
    protected $table = 'sys_form_filter_groups';
    
    /**
     * @var array
     */
    protected $fillable = [
        'form_id',
        'title',
        'uid',
        'can_edit',
        'is_system',
        'order',
        'is_delete',
        'action_type',
        'created_at',
        'updated_at'
    ];
    
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function conditions() {
        return $this->hasMany(EloquentFormFilterGroupField::class, 'filter_group_id', 'id');
    }
    
    
    /**
     * @param     $query
     * @param int $formId
     * @param int $uid
     *
     * @return mixed
     */
    public function scopeUserFilterGroup($query, int $formId, int $uid) {
        return $query->where('form_id', $formId)->where('uid', $uid)->where('is_delete', 0);
    }
    
    /**
     * @param     $query
     * @param int $formId
     *
     * @return mixed
     */
    public function scopeSystemFilterGroup($query, int $formId) {
        return $query->where('form_id', $formId)->where('uid', -1)->where('is_system', 1);
    }
    
}